package org.zhaozhen.pet.controller;


import com.sun.prism.shader.Solid_TextureYV12_AlphaTest_Loader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zhaozhen.basic.Constant.LoginConstant;
import org.zhaozhen.basic.businessexception.BusinessException;
import org.zhaozhen.basic.util.AjaxResult;
import org.zhaozhen.basic.util.LoginContext;
import org.zhaozhen.basic.util.PageList;
import org.zhaozhen.pet.domain.Pet;
import org.zhaozhen.pet.domain.SearchMasterMsg;
import org.zhaozhen.pet.query.SearchMasterMsgQuery;
import org.zhaozhen.pet.service.ISearchMasterMsgService;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    /**
     * 发布寻主
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            seachMasterMsgService.publish(searchMasterMsg, LoginContext.getCurrentLogin(request));
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet,LoginContext.getCurrentLogin(request));
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.queryAll();
    }
    //1.查询待处理寻主消息
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        return seachMasterMsgService.pending(query,LoginContext.getCurrentLogin(request));
    }

    //2.查询已处理寻主消息
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.processed(query,LoginContext.getCurrentLogin(request));
    }

    //3.给主站查询用户自己的寻主消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> queryUserMsg(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.queryUserMsg(query,LoginContext.getCurrentLogin(request));
    }

}
